{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example Usage for DataFrame\n",
    "========================"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# remove comment to use latest development version\n",
    "import sys; sys.path.insert(0, '../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# import libraries\n",
    "import raccoon as rc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initialize\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "object id: 2305959579080\n",
       "columns:\n",
       "[]\n",
       "data:\n",
       "[]\n",
       "index:\n",
       "[]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# empty DataFrame\n",
    "df = rc.DataFrame()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "object id: 2305959578792\n",
       "columns:\n",
       "['a', 'b', 'c']\n",
       "data:\n",
       "[[None, None, None], [None, None, None], [None, None, None]]\n",
       "index:\n",
       "[1, 2, 3]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# with columns and indexes but no data\n",
    "df = rc.DataFrame(columns=['a', 'b', 'c'], index=[1, 2, 3])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "object id: 2305959818248\n",
       "columns:\n",
       "['a', 'b']\n",
       "data:\n",
       "[[1, 2, 3], [4, 5, 6]]\n",
       "index:\n",
       "[10, 11, 12]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# with data\n",
    "df = rc.DataFrame(data={'a': [1, 2, 3], 'b': [4, 5, 6]}, index=[10, 11, 12], columns=['a', 'b'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print\n",
    "-----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     10    1    4\n",
      "     11    2    5\n",
      "     12    3    6\n"
     ]
    }
   ],
   "source": [
    "df.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     10    1    4\n",
      "     11    2    5\n",
      "     12    3    6\n"
     ]
    }
   ],
   "source": [
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setters and Getters\n",
    "-------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# columns\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    first    second\n",
      "-------  -------  --------\n",
      "     10        1         4\n",
      "     11        2         5\n",
      "     12        3         6\n"
     ]
    }
   ],
   "source": [
    "df.columns = ['first', 'second']\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# columns can be renamed with a dict()\n",
    "df.rename_columns({'second': 'b', 'first': 'a'})\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 11, 12]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# index\n",
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index      a    b\n",
      "-------  ---  ---\n",
      "apple      1    4\n",
      "pear       2    5\n",
      "7.7        3    6\n"
     ]
    }
   ],
   "source": [
    "#indexes can be any non-repeating unique values\n",
    "df.index = ['apple', 'pear', 7.7]\n",
    "df.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     10    1    4\n",
      "     11    2    5\n",
      "     12    3    6\n"
     ]
    }
   ],
   "source": [
    "df.index = [10, 11, 12]\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'index'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# the index can also have a name, befault it is \"index\"\n",
    "df.index_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'units'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index_name = 'units'\n",
    "df.index_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[1, 2, 3], [4, 5, 6]]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data is a shallow copy, be careful on how this is used\n",
    "df.index_name = 'index'\n",
    "df.data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Select Index\n",
    "------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[False, True, False]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.select_index(11)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set Values\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     10  100    4\n",
      "     11    2    5\n",
      "     12    3    6\n"
     ]
    }
   ],
   "source": [
    "# set a single cell\n",
    "df.set(10, 'a', 100)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100    4\n",
      "     11    2    5\n",
      "     12    3    6\n",
      "     13              9\n"
     ]
    }
   ],
   "source": [
    "# set a value outside current range creates a new row and/or column. Can also use [] for setting\n",
    "df[13, 'c'] = 9\n",
    "df.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100   55\n",
      "     11    2   55\n",
      "     12    3   55\n",
      "     13        55    9\n"
     ]
    }
   ],
   "source": [
    "# set column\n",
    "df['b'] = 55\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100   66\n",
      "     11    2   55\n",
      "     12    3   66\n",
      "     13        55    9\n"
     ]
    }
   ],
   "source": [
    "# set a subset of column\n",
    "df[[10, 12], 'b'] = 66\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100   88\n",
      "     11    2   55\n",
      "     12    3   99\n",
      "     13        55    9\n"
     ]
    }
   ],
   "source": [
    "# using boolean list\n",
    "df.set([True, False, True, False], 'b', [88, 99])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100   88\n",
      "     11    2   55\n",
      "     12   33   99\n",
      "     13   33   55    9\n"
     ]
    }
   ],
   "source": [
    "# setting with slices\n",
    "df[12:13, 'a'] = 33\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "     10  100   88    1\n",
      "     11    2   55    2\n",
      "     12   33   99    3\n",
      "     13   33   55    9\n"
     ]
    }
   ],
   "source": [
    "df[10:12, 'c'] = [1, 2, 3]\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c    d\n",
      "-------  ---  ---  ---  ---\n",
      "     10  100   88    1\n",
      "     11    2   55    2\n",
      "     12   33   99    3\n",
      "     13   33   55    9\n",
      "     14   44       100   99\n"
     ]
    }
   ],
   "source": [
    "# append a row, DANGEROUS as there is not validation checking, but can be used for speed\n",
    "df.append_row(14, {'a': 44, 'c': 100, 'd': 99})\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c    d\n",
      "-------  ---  ---  ---  ---\n",
      "     10  100   88    1\n",
      "     11    2   55    2\n",
      "     12   33   99    3\n",
      "     13   33   55    9\n",
      "     14   44       100   99\n",
      "     15   55            100\n",
      "     16   56            101\n"
     ]
    }
   ],
   "source": [
    "# append rows, again use caution\n",
    "df.append_rows([15, 16], {'a': [55, 56], 'd': [100,101]})\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Get Values\n",
    "----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get a single cell\n",
    "df[10, 'a']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    c\n",
      "-------  ---\n",
      "     10    1\n",
      "     11    2\n",
      "     12    3\n",
      "     13    9\n",
      "     14  100\n",
      "     15\n",
      "     16\n"
     ]
    }
   ],
   "source": [
    "# get an entire column\n",
    "df['c'].print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    c\n",
      "-------  ---  ---\n",
      "     10  100    1\n",
      "     11    2    2\n",
      "     12   33    3\n",
      "     13   33    9\n",
      "     14   44  100\n",
      "     15   55\n",
      "     16   56\n"
     ]
    }
   ],
   "source": [
    "# get list of columns\n",
    "df[['a', 'c']].print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    b\n",
      "-------  ---\n",
      "     11   55\n",
      "     12   99\n",
      "     13   55\n"
     ]
    }
   ],
   "source": [
    "# get subset of the index\n",
    "df[[11, 12, 13], 'b'].print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    b\n",
      "-------  ---\n",
      "     11   55\n",
      "     12   99\n",
      "     13   55\n"
     ]
    }
   ],
   "source": [
    "# get using slices\n",
    "df[11:13, 'b'].print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    c\n",
      "-------  ---  ---\n",
      "     10  100    1\n",
      "     11    2    2\n"
     ]
    }
   ],
   "source": [
    "# get a matrix\n",
    "df[10:11, ['a', 'c']].print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[100, 2, 33, 33, 44, 55, 56]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get a column, return as a list\n",
    "df.get(columns='a', as_list=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 33, 'b': 55, 'index': 13}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get a row and return as a dictionary\n",
    "df.get_columns(index=13, columns=['a', 'b'], as_dict=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set and Get by Location\n",
    "-----------------------\n",
    "Locations are the index of the index, in other words the index locations from 0...len(index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "33"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get a single cell\n",
    "df.get_location(2, 'a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c  d\n",
      "-------  ---  ---  ---  ---\n",
      "     12   33   99    3\n"
     ]
    }
   ],
   "source": [
    "# get an entire row when the columns is None\n",
    "print(df.get_location(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'b': 88, 'c': 1, 'index': 10}\n"
     ]
    }
   ],
   "source": [
    "print(df.get_location(0, ['b', 'c'], as_dict=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a  b    c      d\n",
      "-------  ---  ---  ---  ---\n",
      "     16   56            101\n"
     ]
    }
   ],
   "source": [
    "df.get_location(-1).print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c  d\n",
      "-------  ---  ---  ---  ---\n",
      "     10  100   88    1\n",
      "     12   33   99    3\n"
     ]
    }
   ],
   "source": [
    "df.get_locations(locations=[0, 2]).print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c    d\n",
      "-------  ---  ---  ---  ---\n",
      "     10   -9   88    1\n",
      "     11    2   55    2\n",
      "     12   -9   99    3\n",
      "     13   33   55    9\n",
      "     14   44       100   99\n",
      "     15   55            100\n",
      "     16   56            101\n"
     ]
    }
   ],
   "source": [
    "df.set_locations(locations=[0, 2], column='a', values=-9)\n",
    "df.print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Head and Tail\n",
    "-------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c  d\n",
      "-------  ---  ---  ---  ---\n",
      "     10   -9   88    1\n",
      "     11    2   55    2\n"
     ]
    }
   ],
   "source": [
    "df.head(2).print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a  b    c      d\n",
      "-------  ---  ---  ---  ---\n",
      "     15   55            100\n",
      "     16   56            101\n"
     ]
    }
   ],
   "source": [
    "df.tail(2).print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Delete colunmns and rows\n",
    "------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c    d\n",
      "-------  ---  ---  ---  ---\n",
      "     11    2   55    2\n",
      "     12   -9   99    3\n",
      "     14   44       100   99\n",
      "     15   55            100\n",
      "     16   56            101\n"
     ]
    }
   ],
   "source": [
    "df.delete_rows([10, 13])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    c    d\n",
      "-------  ---  ---  ---\n",
      "     11    2    2\n",
      "     12   -9    3\n",
      "     14   44  100   99\n",
      "     15   55       100\n",
      "     16   56       101\n"
     ]
    }
   ],
   "source": [
    "df.delete_columns('b')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Convert\n",
    "-------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'index': [11, 12, 14, 15, 16],\n",
       " 'a': [2, -9, 44, 55, 56],\n",
       " 'c': [2, 3, 100, None, None],\n",
       " 'd': [None, None, 99, 100, 101]}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# return a dict\n",
    "df.to_dict()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': [2, -9, 44, 55, 56],\n",
       " 'c': [2, 3, 100, None, None],\n",
       " 'd': [None, None, 99, 100, 101]}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# exclude the index\n",
    "df.to_dict(index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OrderedDict([('index', [11, 12, 14, 15, 16]),\n",
       "             ('a', [2, -9, 44, 55, 56]),\n",
       "             ('c', [2, 3, 100, None, None]),\n",
       "             ('d', [None, None, 99, 100, 101])])"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# return an OrderedDict()\n",
    "df.to_dict(ordered=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 3, 100, None, None]"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# return a list of just one column\n",
    "df['c'].to_list()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"data\": {\"a\": [2, -9, 44, 55, 56], \"c\": [2, 3, 100, null, null], \"d\": [null, null, 99, 100, 101]}, \"index\": [11, 12, 14, 15, 16], \"meta_data\": {\"index_name\": \"index\", \"columns\": [\"a\", \"c\", \"d\"], \"sort\": false, \"dropin\": null}}\n"
     ]
    }
   ],
   "source": [
    "# convert to JSON\n",
    "string = df.to_json()\n",
    "print(string)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    c    d\n",
      "-------  ---  ---  ---\n",
      "     11    2    2\n",
      "     12   -9    3\n",
      "     14   44  100   99\n",
      "     15   55       100\n",
      "     16   56       101\n"
     ]
    }
   ],
   "source": [
    "# construct DataFrame from JSON\n",
    "df_from_json = rc.DataFrame.from_json(string)\n",
    "print(df_from_json)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sort by Index and Column\n",
    "------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     25    4    6\n",
      "     24    3    7\n",
      "     23    2    8\n",
      "     22    1    9\n"
     ]
    }
   ],
   "source": [
    "df = rc.DataFrame({'a': [4, 3, 2, 1], 'b': [6, 7, 8, 9]}, index=[25, 24, 23, 22])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     22    1    9\n",
      "     23    2    8\n",
      "     24    3    7\n",
      "     25    4    6\n"
     ]
    }
   ],
   "source": [
    "# sort by index. Sorts are inplace\n",
    "df.sort_index()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     25    4    6\n",
      "     24    3    7\n",
      "     23    2    8\n",
      "     22    1    9\n"
     ]
    }
   ],
   "source": [
    "# sort by column\n",
    "df.sort_columns('b')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     22    1    9\n",
      "     23    2    8\n",
      "     24    3    7\n",
      "     25    4    6\n"
     ]
    }
   ],
   "source": [
    "# sort by column in reverse order\n",
    "df.sort_columns('b', reverse=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sorting with a key function is avaialble, see tests for examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Append\n",
    "------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "      1    1    5\n",
      "      2    2    6\n"
     ]
    }
   ],
   "source": [
    "df1 = rc.DataFrame({'a': [1, 2], 'b': [5, 6]}, index=[1, 2])\n",
    "df1.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    b    c\n",
      "-------  ---  ---\n",
      "      3    7   11\n",
      "      4    8   12\n"
     ]
    }
   ],
   "source": [
    "df2 = rc.DataFrame({'b': [7, 8], 'c': [11, 12]}, index=[3, 4])\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b    c\n",
      "-------  ---  ---  ---\n",
      "      1    1    5\n",
      "      2    2    6\n",
      "      3         7   11\n",
      "      4         8   12\n"
     ]
    }
   ],
   "source": [
    "df1.append(df2)\n",
    "print(df1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Math Methods\n",
    "------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 3], 'b': [2, 8, 9]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[False, False, True]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# test for equality\n",
    "df.equality('a', value=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[False, False]"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# all math methods can operate on a subset of the index\n",
    "df.equality('b', indexes=[1, 2], value=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 10, 12]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# add two columns\n",
    "df.add('a', 'b')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 6, 6]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# subtract\n",
    "df.subtract('b', 'a')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 27]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# multiply\n",
    "df.multiply('a', 'b', [0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2.0, 4.0, 3.0]"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# divide\n",
    "df.divide('b', 'a')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Multi-Index\n",
    "-----------\n",
    "Raccoon does not have true hierarchical mulit-index capabilities like Pandas, but attempts to mimic some of the capabilities with the use of tuples as the index. Raccoon does not provide any checking to make sure the indexes are all the same length or\n",
    "any other integrity checking."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "index          a\n",
      "-----------  ---\n",
      "('a', 1, 3)    1\n",
      "('a', 1, 4)    2\n",
      "('a', 2, 3)    3\n",
      "('b', 1, 4)    4\n",
      "('b', 2, 1)    5\n",
      "('b', 3, 3)    6\n"
     ]
    }
   ],
   "source": [
    "tuples = [('a', 1, 3), ('a', 1, 4), ('a', 2, 3), ('b', 1, 4), ('b', 2, 1), ('b', 3, 3)]\n",
    "df = rc.DataFrame({'a': [1, 2, 3, 4, 5, 6]}, index=tuples)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The select_index method works with tuples by allowing the * to act as a wild card for matching."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, True, True, False, False, False]"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = ('a', None, None)\n",
    "df.select_index(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, False, True, False, False, False]"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = ('a', None, 3)\n",
    "df.select_index(compare, 'boolean')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 2, 3), ('b', 2, 1)]"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = (None, 2, None)\n",
    "df.select_index(compare, 'value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('a', 1, 3), ('a', 2, 3), ('b', 3, 3)]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = (None, None, 3)\n",
    "df.select_index(compare, 'value')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[True, True, True, True, True, True]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare = (None, None, None)\n",
    "df.select_index(compare)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reset Index\n",
    "-----------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "      0    1    4\n",
      "      1    2    5\n",
      "      2    3    6\n"
     ]
    }
   ],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'])\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "object id: 2305960012584\n",
       "columns:\n",
       "['a', 'b', 'index_0']\n",
       "data:\n",
       "[[1, 2, 3], [4, 5, 6], [0, 1, 2]]\n",
       "index:\n",
       "[0, 1, 2]"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.reset_index()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jelo      a    b\n",
      "------  ---  ---\n",
      "x         1    4\n",
      "y         2    5\n",
      "z         3    6\n"
     ]
    }
   ],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=['x', 'y', 'z'], index_name='jelo')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b  jelo\n",
      "-------  ---  ---  ------\n",
      "      0    1    4  x\n",
      "      1    2    5  y\n",
      "      2    3    6  z\n"
     ]
    }
   ],
   "source": [
    "df.reset_index()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('melo', 'helo', 'gelo')      a    b\n",
      "--------------------------  ---  ---\n",
      "('a', 10, 'x')                1    4\n",
      "('b', 11, 'y')                2    5\n",
      "('c', 12, 'z')                3    6\n"
     ]
    }
   ],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'],\n",
    "                   index=[('a', 10, 'x'), ('b', 11, 'y'), ('c', 12, 'z')], index_name=('melo', 'helo', 'gelo'))\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b  melo      helo  gelo\n",
      "-------  ---  ---  ------  ------  ------\n",
      "      0    1    4  a           10  x\n",
      "      1    2    5  b           11  y\n",
      "      2    3    6  c           12  z\n"
     ]
    }
   ],
   "source": [
    "df.reset_index()\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jelo      a    b\n",
      "------  ---  ---\n",
      "x         1    4\n",
      "y         2    5\n",
      "z         3    6\n"
     ]
    }
   ],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]}, columns=['a', 'b'], index=['x', 'y', 'z'], index_name='jelo')\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "      0    1    4\n",
      "      1    2    5\n",
      "      2    3    6\n"
     ]
    }
   ],
   "source": [
    "df.reset_index(drop=True)\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Iterators\n",
    "---------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = rc.DataFrame({'a': [1, 2, 'c'], 'b': [5, 6, 'd']}, index=[1, 2, 3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'index': 1, 'a': 1, 'b': 5}\n",
      "{'index': 2, 'a': 2, 'b': 6}\n",
      "{'index': 3, 'a': 'c', 'b': 'd'}\n"
     ]
    }
   ],
   "source": [
    "for row in df.iterrows():\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Raccoon(index=1, a=1, b=5)\n",
      "Raccoon(index=2, a=2, b=6)\n",
      "Raccoon(index=3, a='c', b='d')\n"
     ]
    }
   ],
   "source": [
    "for row in df.itertuples():\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Sorted DataFrames\n",
    "-----------------\n",
    "DataFrames will be set to sorted by default if no index is given at initialization. If an index is given at initialization then the parameter sorted must be set to True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = rc.DataFrame({'a': [3, 5, 4], 'b': [6, 8, 7]}, index=[12, 15, 14], sort=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When sorted=True on initialization the data will be sorted by index to start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     12    3    6\n",
      "     14    4    7\n",
      "     15    5    8\n"
     ]
    }
   ],
   "source": [
    "df.print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     12    3    6\n",
      "     14    4    7\n",
      "     15    5    8\n",
      "     16         9\n"
     ]
    }
   ],
   "source": [
    "df[16, 'b'] = 9\n",
    "print(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  index    a    b\n",
      "-------  ---  ---\n",
      "     12  3    6\n",
      "     13  3.5  6.5\n",
      "     14  4    7\n",
      "     15  5    8\n",
      "     16       9\n"
     ]
    }
   ],
   "source": [
    "df.set(indexes=13, values={'a': 3.5, 'b': 6.5})\n",
    "print(df)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:.conda-raccoon]",
   "language": "python",
   "name": "conda-env-.conda-raccoon-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
